
import 'package:flutter/material.dart';
import 'package:flutter_ohos/app/routes/app_pages.dart';
import 'package:flutter_ohos/app/routes/app_routes.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import 'counter_method_channel.dart';

// @RoutePage()
class ContentView extends StatefulWidget {
  String? account;
  ContentView({super.key, this.account});

  @override
  State<ContentView> createState() => _ContentViewState();
}

class _ContentViewState extends State<ContentView> {

  ///登录
  Future<void> _login() async {
    //点击登录调取鸿蒙原生登录页面
    Channel.methodChannel
        .invokeMethod<String?>('login');
    //等待接收华为账号一键登录后的用户信息，目前只接收了昵称
    Channel.methodChannel.setMethodCallHandler((call) =>
        Future(() {
          if (call.method == 'loginResult') {
            widget.account = call.arguments;
            setState(() {

            });
          }
        }));
  }

  ///路由跳转
  _routeTo () {
    Get.toNamed(Routes.FOURTHVIEW,arguments: {"gender":"女"});
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      // Center is a layout widget. It takes a single child and positions it
      // in the middle of the parent.
      child: Column(
        // Column is also a layout widget. It takes a list of children and
        // arranges them vertically. By default, it sizes itself to fit its
        // children horizontally, and tries to be as tall as its parent.
        //
        // Invoke "debug painting" (press "p" in the console, choose the
        // "Toggle Debug Paint" action from the Flutter Inspector in Android
        // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
        // to see the wireframe for each widget.
        //
        // Column has various properties to control how it sizes itself and
        // how it positions its children. Here we use mainAxisAlignment to
        // center the children vertically; the main axis here is the vertical
        // axis because Columns are vertical (the cross axis would be
        // horizontal).
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text("Hello World!",style: TextStyle(fontSize: 16,color: Colors.blue,fontWeight: FontWeight.bold),),
          Visibility(
            visible: (widget.account??'').isNotEmpty,
              child: Text("华为账号昵称：${widget.account??''}")
          ),
          TextButton(
              style: ButtonStyle(
                //字体样式
                  textStyle: MaterialStateProperty.all(const TextStyle(
                      color: Colors.white,
                      fontSize: 14,
                      fontWeight: FontWeight.w700)),
                  backgroundColor:
                  MaterialStateProperty.all(Colors.green),
                  //阴影效果
                  elevation: MaterialStateProperty.all(0),
                  //设置圆角、边框样式
                  shape: MaterialStateProperty.all(
                    RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8)),
                  )),
              onPressed: _login,
              child: const Text("登录跳转到鸿蒙原生页",style: TextStyle(color: Colors.white),)
          ),
          TextButton(
              style: ButtonStyle(
                //字体样式
                  textStyle: MaterialStateProperty.all(const TextStyle(
                      color: Colors.white,
                      fontSize: 14,
                      fontWeight: FontWeight.w700)),
                  backgroundColor:
                  MaterialStateProperty.all(Colors.green),
                  //阴影效果
                  elevation: MaterialStateProperty.all(0),
                  //设置圆角、边框样式
                  shape: MaterialStateProperty.all(
                    RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8)),
                  )),
              onPressed: _routeTo,
              child: const Text("路由跳转验证",style: TextStyle(color: Colors.white),)
          ),
        ],
      ),
    );
  }
}
